package com.kushe.dao;

import com.kushe.entity.Images;
import com.kushe.util.core.DataAccess;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;

import java.util.List;

/**
 * Created by WhiteBlue on 15/5/15.
 */
public class ImagesDao extends DataAccess<Images> {

    @Override
    public Images get(int id) {
        return getDao().fetch(Images.class, id);
    }


    public Images addImages(String fileName, int userId, String title) {
        Images images = new Images();
        images.setUser_id(userId);
        images.setImg(fileName);
        images.setTitle(title);
        getDao().insert(images);
        return images;
    }


    public QueryResult listByPage(int pageNumber) {
        Pager pager = getDao().createPager(pageNumber, 8);
        List<Images> list = getDao().query(Images.class, Cnd.orderBy().desc("id"), pager);
        return new QueryResult(list, pager);
    }

    /**
     * 得到用户信息上显示的八个素材
     *
     * @param user_id 用户的id
     */
    public List<Images> getForUserLimit(int user_id) {
        List<Images> list = getDao().query(Images.class, Cnd.where("user_id", "=", user_id).limit(8));
        return list;
    }


    /**
     * 得到添加素材页面的八个最新素材
     */
    public List<Images> getForHot() {
        List<Images> list = getDao().query(Images.class, Cnd.limit().limit(4).desc("count"));
        return list;
    }

    /**
     * 得到首页的最热素材
     */
    public List<Images> getForIndex() {
        List<Images> list = getDao().query(Images.class, Cnd.limit().limit(4).desc("count"));
        return list;
    }


}
